class Item extends HtmlNode{
    constructor({name,required,icon,versions,details}) {
        super('tr.file.layui-row');
        let $this=this;
        //创建图标
        let iconTdEL=new HtmlNode('td[width=32px]',this)
        let iconEl=new HtmlNode(`img[width=32px][src=${icon}]`,iconTdEL)

        //创建文件名
        let filename=new HtmlNode("td[width=100px]",this)
        filename.css("padding-left","5px")
        filename.bindTextContent(this,'filename')
        this.filename=name
        // 是否必须
        let requiredEl=new HtmlNode('span.layui-badge',new HtmlNode("td[width=50px]",this))
        Object.defineProperty(this,'required',{
            enumerable:true,
            configurable:true,
            get(){
                return !requiredEl.hasClass('layui-bg-gray')
            },
            set(value){
                if (value&&requiredEl.hasClass("layui-bg-gray")){
                    requiredEl.toggleClass("layui-bg-gray")
                }else if (!value&&!requiredEl.hasClass("layui-bg-gray")){
                    requiredEl.toggleClass("layui-bg-gray")
                }
                requiredEl.textContent=value?'必须':'可选'
            }
        })
        this.required=required
        // <td class="layui-col-md5">文件详情</td>
        let detailsEl=new HtmlNode("td",this)
        detailsEl.bindTextContent(this,'details')
        this.details=details
        //创建版本列表
        this.versions=new Versions(versions&&versions.map(v=>new Version(v)))
        this.appendChild(this.versions)

        //创建按钮
        let btn=new HtmlNode('button.layui-btn[type=button]',new HtmlNode('td[width=100px]',this))
        btn.textContent='下载'
        let a = document.createElement("a");
        btn.addEventListener('click',e=>{
            let version=this.versions.selectVersion
            if(version.dataset.pwd){
                layui.layer.open({
                    title: '下载地址'
                    ,content: `下载地址:<a href="${version.value}" target="_blank" title="点击跳转到网盘下载">${version.value}</a><br> 提取码:<span class="tqm">${version.dataset.pwd}</span>`
                    ,btn:['去网盘下载']
                    ,yes(index){
                        copyText(version.dataset.pwd)
                        a.href = version.value;
                        a.target='_blank'
                        a.click()
                        layui.layer.close(index);
                    }
                });
            }else{
                a.href = version.value;
                a.download = this.filename+version.type
                a.click();
            }
        })
    }
}